﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DatabaseHelper.OpenGetSqlConnection();
            //将自己存入保存From字典中
            if (!RuningData.FormDir.ContainsKey(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name))
            {
                RuningData.FormDir.Add(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, this);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("SELECT COUNT(*) FROM tb_User WHERE UserName='{0}' AND UserPwd='{1}'", textBox1.Text, textBox2.Text);
            int count = Convert.ToInt32(DatabaseHelper.ExecuteScalar(sql));
            if (count > 0)
            {
                //跳转页面
                string selectUserSql = string.Format("SELECT * FROM tb_User WHERE UserName='{0}' AND UserPwd='{1}'", textBox1.Text, textBox2.Text);
                SqlDataReader sdr = DatabaseHelper.GetSqlDataReader(selectUserSql);
                string username = null;
                string power = null;
                if (sdr.HasRows)
                {
                    while (sdr.Read())
                    {
                        username = sdr["UserName"].ToString();
                        power = sdr["power"].ToString();
                    }
                }
                sdr.Close();
                Form2 f2 = new Form2();
                f2.setUsername(username);
                f2.setPower(power);
                f2.setLogonTime(new DateTime());
                this.Hide();
                f2.Show();
            }
            else
            {
                MessageBox.Show("账号或者用户名错误!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                textBox1.Focus();
                textBox1.Text = string.Empty;
                textBox2.Text = string.Empty;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认退出!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
            {
                Application.Exit();
            }
        }

    }
}
